package com.example.wudi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.wudi.entity.User;
import com.example.wudi.mapper.UserMapper;
import com.example.wudi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> findAll() {
        final List<User> users = userMapper.selectList(null);

        return users;
    }

    @Override
    public User login(String userName, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        queryWrapper.eq("pass_word", password);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public User findUserById(int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        final User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public int addUser(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", user.getUserName());
        queryWrapper.eq("appellation", user.getAppellation());
        final User user1 = userMapper.selectOne(queryWrapper);
        if(user1 != null) {
            return  0;
        }
        return userMapper.insert(user);
    }

    @Override
    public int update(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", user.getId());
        userMapper.update(user, queryWrapper);
        return userMapper.update(user, queryWrapper);
    }

    @Override
    public int delete(int id) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        userMapper.delete(queryWrapper);
        return userMapper.delete(queryWrapper);
    }


}
